from rich import print
from rich.table import Table

# 学生类
class Student:
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade

    def __str__(self):
        return f"姓名：{self.name}, 年龄：{self.age}, 年级：{self.grade}"

# 学生管理系统类
class StudentManager:
    def __init__(self):
        self.students = []

    def add_student(self, name, age, grade):
        # 添加学生
        student = Student(name, age, grade)
        self.students.append(student)
        print(f"添加学生：{student}")

    def delete_student(self, name):
        # 删除学生
        for student in self.students:
            if student.name == name:
                self.students.remove(student)
                print(f"删除学生：{name}")
                return
        print(f"没有找到学生：{name}")

    def update_student(self, name, age=None, grade=None):
        # 更新学生信息
        for student in self.students:
            if student.name == name:
                if age:
                    student.age = age
                if grade:
                    student.grade = grade
                print(f"更新学生：{student}")
                return
        print(f"没有找到学生：{name}")

    def search_student(self, name):
        # 搜索学生
        for student in self.students:
            if student.name == name:
                print(f"找到学生：{student}")
                return
        print(f"没有找到学生：{name}")

    def display_students(self):
        # 显示所有学生
        for student in self.students:
            print(student)

    def showMenu(self):
        # 显示菜单
        # 打印菜单
        table = Table(title="学生管理系统")
        table.add_column("序号", style="magenta", justify="center",no_wrap=True,width=20)
        table.add_column("功能", style="magenta", justify="center",no_wrap=True,width=200)

        table.add_row("1", "添加学生")
        table.add_row("2", "删除学生")
        table.add_row("3", "更新学生信息")
        table.add_row("4", "搜索学生")
        table.add_row("5", "显示所有学生")
        table.add_row("6", "退出系统")
        print(table)


# 主函数
def main():
    manager = StudentManager()

    while True:
        # 打印菜单
        manager.showMenu()


        choice = input("请输入您的选择：")

        if choice == "1":
            name = input("请输入学生姓名：")
            age = int(input("请输入学生年龄："))
            grade = input("请输入学生年级：")
            manager.add_student(name, age, grade)
        elif choice == "2":
            name = input("请输入学生姓名：")
            manager.delete_student(name)
        elif choice == "3":
            name = input("请输入学生姓名：")
            age = input("请输入学生年龄（留空则不更新）：")
            grade = input("请输入学生年级（留空则不更新）：")
            if age:
                age = int(age)
            else:
                age = None
            if grade:
                grade = grade
            else:
                grade = None
            manager.update_student(name, age, grade)
        elif choice == "4":
            name = input("请输入学生姓名：")
            manager.search_student(name)
        elif choice == "5":
            manager.display_students()
        elif choice == "6":
            print("退出系统")
            break
        else:
            print("无效选择，请重新输入")

if __name__ == "__main__":
    main()
